class CircularLink:
    """循环链表"""

    # ---------------------栈类---------------------
    class _Node:
        __slots__ = 'element', 'next'

        def __init__(self, element, next_):
            self.element = element
            self.next = next_

    # ---------------------栈类---------------------

    def __init__(self):
        self.tail = None
        self.size = 0

    def len(self):
        return self.size

    def is_empty(self):
        return self.size == 0

    def add(self, data):
        """Add a data to tail next"""
        if self.is_empty():
            self.tail = self._Node(data, self.tail)
            self.tail.next = self.tail
        else:
            newest = self._Node(data, self.tail.next)
            self.tail.next = newest
        self.size += 1

    def rotate(self):
        if self.is_empty():
            raise Exception('栈无内容')
        self.tail = self.tail.next
        return self.tail.element

    def current(self):
        if self.is_empty():
            raise Exception('栈无内容')
        return self.tail.next.element

    def pop(self):
        """删除并返回当前值"""
        if self.is_empty():
            raise Exception('栈无内容')
        data = self.tail.next.element
        old_head = self.tail.next
        if self.size == 1:
            self.tail = None
        else:
            self.tail.next = old_head.next
        self.size -= 1
        return data

    def __str__(self):
        data = []
        current = self.tail.next
        for _ in range(self.size):
            data.append(current.element)
            current = current.next
        return str(data)


if __name__ == '__main__':
    link1 = CircularLink()
    for i in range(10):
        link1.add(i)
    print(link1)
    link1.pop()
    link1.pop()
    link1.pop()
    print(link1.current())
    link1.rotate()
    link1.rotate()
    link1.rotate()
    print(link1.current())
    print(link1)